package com.gxuwz.leaveSystem.controller;

import com.gxuwz.leaveSystem.entity.Student;
import com.gxuwz.leaveSystem.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
@RestController
public class StudentController {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    StudentService service = (StudentService) applicationContext.getBean("studentService");

    /**
     * 删除
     * @param id
     */
    @RequestMapping("/stu_delete")
    public ModelAndView delete(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/stu_list");
        try {
            int num = service.deleteStudent(id);
            if (num == 1){
                System.out.println("删除成功");
                return mv;
            }else {
                System.out.println("删除失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("删除异常");
        }
        return mv;
    }
    /**
     * 修改
     * @param student
     * @return
     */
    @RequestMapping("/stu_edit")
    public ModelAndView update(Student student){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/stu_list");
        try {
            int num = service.updateStudent(student);
            if (num==1){
                System.out.println("更新成功");
                return mv;
            }else {
                System.out.println("更新失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("更新异常");
        }

        return mv;
    }

    /**
     * 转到编辑页面
     * @param id
     * @return
     */
    @RequestMapping("/stu_editView")
    public ModelAndView editView(String id){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/student/stu_edit");
        try {
            Student student =service.getStudent(id);
            mv.addObject("student",student);
            return mv;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找异常");
        }
        return mv;
    }


    /**
     * 添加
     * 判断
     * 重定向到列表页面
     * @param student
     * @return
     */
    @RequestMapping("/stu_add")
    public ModelAndView add(Student student){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("forward:/stu_list");
        try {
            int num = service.addStudent(student);
            if (num == 1){
                System.out.println("添加成功");
                return mv;
            }else {
                System.out.println("添加失败");
                return mv;
            }
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("添加异常");
        }
        return mv;
    }

    /**
     * 定向到添加页面
     * @return
     */
    @RequestMapping("/stu_addView")
    public ModelAndView addView(){
        ModelAndView mv = new ModelAndView();
        Student student = new Student("20220951228","2022","马至桔","男","gxuwz","13347509284","马至桔","12345678911");
        mv.addObject("student",student);
        mv.setViewName("/student/stu_add");
        return mv;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/stu_list")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/student/stu_list");
        try {
            List<Student> students = service.getAllStudents();
            mv.addObject("student",students);
            return mv;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("查找列表异常");
        }
        return mv;
    }

    /**
     * 根据关键字搜索
     * @param keyword
     * @return
     */
    @RequestMapping("/stu_search")
    public ModelAndView search(String keyword) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/student/stu_list");
        try {
            List<Student> students = service.searchStudent(keyword);
            if (keyword!=null&&students!=null){
                mv.addObject("student",students);
                return mv;
            }else {
                System.out.println("查找不到或没有！");
                return mv;
            }
        }catch (Exception e){
            System.out.println("搜索异常");
        }
        return mv;
    }
}
